<?php

namespace App\Model\Task;

use App\Constants\TaskStatus;
use App\Model\Model;
use App\Model\System\SystemFile;
use App\Model\User\User;
use Carbon\Carbon;
use Hyperf\Crontab\Crontab;
use Hyperf\Database\Model\SoftDeletes;

/**
 * @property integer $id
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $delete_time
 * @property string $title
 * @property integer $admin_id
 * @property decimal $price
 * @property string $info
 * @property string $desc
 * @property string $notice
 */
class Task extends Model
{
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'task';

    protected $fillable = [
        'create_time',
        'update_time',
        'delete_time',
        'title',
        'admin_id',
        'price',
        'one_price',
        'recomment_time',
        'recomment_reason',
        'user_id',
        'is_open',
        'view',
        'product_name',
        'device',
        'submit_cycle',
        'audit_cycle',
        'stock',
        'total_stock',
        'info',
        'end_time',
        'desc',
        'pay_status',
        'finish',
        'reason',
        'notice',
        'status',
        'image_id',
        'types',
        'refresh_time',
        'top_time',
        'task_types_id',
    ];

    protected $appends = [
        'create_date',
        'end_time_format',
        'status_format',
        'beans',
    ];

    public function getBeansAttribute()
    {
        return $this->checkAttributes('one_price', function ($one_price) {
            return bcmul($one_price, 10);
        });
    }

    public function getStatusFormatAttribute()
    {
        return $this->checkAttributes('status', function ($status) {
            dump($status);
            return TaskStatus::getMessage($status);
        });
    }

    public function getUnFinishNumAttribute()
    {
        return $this->checkAttributes('id', function ($id) {
            return App(TaskReceive::class)->where('status', 10)->where('task_id', $id)->count();
        });
    }

    public function getCreateDateAttribute()
    {
        return $this->checkAttributes('create_time', function ($create_time) {
            return date('Y-m-d', $create_time);
        });
    }

    public function TaskTypes()
    {
        return $this->hasOne(TaskTypes::class, 'id', 'task_types_id');
    }

    public function getEndTimeFormatAttribute()
    {
        return $this->checkAttributes('end_time', function ($end_time) {
            return date('Y-m-d', $end_time);
        });
    }

    public function setEndTimeAttribute($value)
    {
        dump($value);
        $this->attributes['end_time'] = \Yj\Tool\Carbon::date($value)->endOfDay()->timestamp;
    }

    public function User()
    {
        return $this->hasOne(User::class, 'uid', 'user_id');
    }

    public function Image()
    {
        return $this->hasOne(SystemFile::class, 'id', 'image_id')->withDefault(function () {
            return App(SystemFile::class)->where('id', '455')->first();
        });
    }

    public function TaskSteps()
    {
        return $this->hasMany(TaskSteps::class, 'task_id', 'id');
    }
}
